<template>
  <div class="system-user-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="540">
      <el-form ref="formRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="100px">
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="state.ruleForm.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="项目描述" prop="summary">
          <el-input v-model="state.ruleForm.summary" type="textarea" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="state.ruleForm.price" placeholder="请输入"></el-input-number> （元）
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="state.ruleForm.unit" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="总数量" prop="total">
          <el-input-number v-model="state.ruleForm.total"></el-input-number>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSubmit" size="default" :loading="state.submit.loading">保存</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="addActivityItem">
import { nextTick, reactive, ref } from 'vue';
import { Merchant } from '/@/models';
import { DialogType } from '/@/constants/dialog-type';
import { createActivityItem, updateActivityItem } from '/@/api/activity-item';
import { ElMessage } from 'element-plus';
import { assginDefault } from '/@/utils/assign-default';
import { cloneDeep } from 'lodash/fp';
import { createRequiredRule } from '/@/utils';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const props = defineProps({
  activityId: {
    type: String,
    required: true,
    default: ''
  }
});

// 定义变量内容
const formRef = ref();
const state = reactive({
  id: '',
  activityId: '',
  ruleForm: {
    name: '',
    price: 0,
    summary: '',
    unit: '人',
    total: 0
  },
  rules: {
    name: createRequiredRule(),
    price: createRequiredRule(),
    summary: createRequiredRule(),
    unit: createRequiredRule(),
    total: createRequiredRule()
  },
  roleList: [],
  submit: {
    loading: false
  },
  dialog: {
    isShowDialog: false,
    type: '',
    title: ''
  },
  imageUrl: ''
});

// 打开弹窗
const openDialog = (type: string, row?: Merchant) => {
  state.dialog.type = type;
  state.dialog.isShowDialog = true;
  nextTick(() => {
    if (type === DialogType.EDIT) {
      state.id = row.id;
      assginDefault(state.ruleForm, cloneDeep(row));
      state.dialog.title = '修改活动项';
    } else {
      state.id = '';
      state.dialog.title = '新增活动项目';
      formRef.value?.resetFields();
    }
  });
};

// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};

// 取消
const onCancel = () => {
  closeDialog();
};

// 提交
const onSubmit = async () => {
  await formRef.value.validate();
  try {
    state.submit.loading = true;
    const data: any = {
      ...state.ruleForm
    };
    if (state.dialog.type === DialogType.EDIT) {
      await updateActivityItem(state.id, data);
    } else {
      data.activity_id = props.activityId;
      await createActivityItem(data);
    }
    ElMessage.success('保存成功');
    closeDialog();
    emit('refresh');
  } catch (err) {
    console.log(err);
  }

  state.submit.loading = false;
};

// 暴露变量
defineExpose({
  openDialog,
  closeDialog
});
</script>
